package com.shen.members;

import java.util.HashMap;
import java.util.Map;

/**
 * Author:shen
 * CreateDate:2020/5/8.
 * Function:
 */
public class MemberSystem {
    private static Map<Integer,Member> memberMap=new HashMap<>();
    private static int id=3;
    static {
        memberMap.put(1,new Member(101,0));
        memberMap.put(2,new Member(102,10));
        memberMap.put(3,new Member(103,100));
    }
    /**
     * 查询用户
     */
    public int queryMemberId(int memberId){
        int id=-1;
        for (Map.Entry<Integer,Member> k:memberMap.entrySet()) {
            if (k.getValue().getMemberId()==memberId){
                id= k.getKey();
            }
        }
      return id;
    }
    /**
     * 涨积分
     */
    public boolean increCount(int memberId,int integral){
        boolean isflag=false;
        int id=queryMemberId(memberId);
        if(id!=-1){
            isflag=true;
            memberMap.get(id).setCount(memberMap.get(id).getCount()+integral);
        }
        return isflag;
    }
    /**
     *添加会员
     */
    public String addMember(Member member){
        String msg="用户id重复，请重新输入";
        if(queryMemberId(member.getMemberId())==-1){
            memberMap.put(id++,member);
            msg="添加成功！";
        }
        return msg;
    }

    /**
     * 查询所有用户信息
     */
    public void queryAll(){
        System.out.println(memberMap.toString());
    }
    /**
     *兑换礼物
     */
    public String exchangeGift(int memberId,Gift gift){
        String msg="兑换失败";
        int id=queryMemberId(memberId);
        Member member=memberMap.get(id);
        if(id!=-1){
            for (Gift k: Gift.values()) {
                if(gift==k){
                    if(member.getCount()>=k.getValue()) {
                        member.setCount(memberMap.get(id).getCount() - k.getValue());
                        msg = "兑换成功";
                    }
                }
            }
        }else{
            msg="用户不存在";
        }
        return msg;
    }


}
